<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-row type="flex" justify="space-between" style="width: 100%">
          <el-col :span="20">
            <el-form
              :inline="true"
              :model="formInline"
              class="demo-form-inline"
              ref="formInlineRef"
            >
              <el-form-item label="标签名称" prop='tagName'>
                <el-input v-model="formInline.tagName"></el-input>
              </el-form-item>
              <el-form-item label="状态" prop="state">
                <el-select v-model="formInline.state" placeholder="请选择">
                  <el-option
              v-for="item in status"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button @click = 'resetForm'>清除</el-button>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="4" style="text-align: right">
            <el-button type="success small" @click="showDialog"
              ><i class="el-icon-edit"></i>新增标签</el-button
            >
          </el-col>
        </el-row>

        <el-alert
          :title="title"
          type="info"
          show-icon
          :closable="false"
          style="font-size: 14px"
        >
        </el-alert>
        <el-table :data="list" style="width: 100%">
          <el-table-column type="index" label="序号" width="80">
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科" >
          </el-table-column>
          <el-table-column prop="tagName" label="标签名称" > </el-table-column>
          <el-table-column prop="username" label="创建者" >
          </el-table-column>
          <el-table-column prop="addDate" label="创建日期" >
            <template #default="{ row }">
              {{ dateFormat(row.addDate) }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态" >
            <template #default="{ row }">
              {{ tagEnum[row.state] }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150" >
            <template #default="{row}">
              <el-button type="text" @click="changeState(row)" >{{row.state?'禁用':'启用'}}</el-button>
              <el-button type="text" :disabled="row.state===1" @click="editTag(row.id)">修改</el-button>
              <el-button type="text" @click="del(row.id)" :disabled="row.state===1">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row
          type="flex"
          justify="end"
          align="middle"
          style="margin-top: 20px"
        >
          <el-pagination
            background
            layout="prev, pager, next,sizes,jumper"
            :total="counts"
            :page-size="params.pagesize"
            :page-sizes=[5,10,20,50]
            :current-page="params.page"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          >
          </el-pagination>
        </el-row>
      </el-card>
    </div>
    <tags-add
    ref="dialogRef"
    :tagID="tagID"
    :dialogVisible="dialogVisible"
    @input="closeDialog"
    @get-taglist ='getTagList'
    ></tags-add>
  </div>
</template>

<script>
import { status } from '@/api/hmmm/constants.js'
import { list, changeState, remove } from '@/api/hmmm/tags.js'
import TagsAdd from '@/module-hmmm/components/tags-add.vue'
import dayjs from 'dayjs'
export default {
  components: {
    TagsAdd
  },
  data () {
    return {
      formInline: {
        tagName: '',
        state: ''
      },
      list: [],
      params: {
        page: 1,
        pagesize: 10
      },
      counts: 0,
      tagEnum: {
        1: '已启用',
        0: '已禁用'
      },
      title: '',
      dialogVisible: false,
      status: status,
      tagID: 0
    }
  },
  created () {
    this.getTagList()
  },
  methods: {
    async getTagList () {
      const res = await list(this.params)
      // console.log(res)
      this.list = res.data.items
      this.counts = res.data.counts
      this.title = `数据一共${this.counts}条`
    },
    dateFormat (str) {
      return dayjs(str).format('YYYY-MM-DD HH:mm:ss')
    },
    handleCurrentChange (val) {
      this.params.page = val
      this.getTagList()
    },
    handleSizeChange (val) {
      this.params.pagesize = val
      this.getTagList()
    },
    showDialog () {
      this.dialogVisible = true
    },
    closeDialog () {
      this.dialogVisible = false
      this.tagID = 0
    },
    resetForm () {
      this.$refs.formInlineRef.resetFields()
      this.getTagList()
    },
    async onSubmit () {
      if (this.formInline.tagName === '' && this.formInline.state === '') {
        this.getTagList()
      } else {
        const res = await list({ ...this.formInline, ...this.params })
        // console.log(res)
        this.list = res.data.items
        this.counts = res.data.counts
        this.title = `数据一共${this.counts}条`
      }
    },
    async changeState (row) {
      // console.log(row, row.id)
      // console.log(id)
      if (row.state === 1) {
        row.state = 0
      //  this.disabled = true
      } else {
        row.state = 1
        // this.disabled = false
      }
      // const arr = this.list.filter(item => item.id === row.id
      // 发起请求，更新后台数据
      await changeState(row)
    },
    async del (id) {
      // 询问用户
      this.$confirm('此操作将永久删除该权限点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 发起删除请求
        await remove({ id: id })
        // 提示用户
        this.$message.success('删除成功')
        // 刷新表格数据
        this.getTagList()
      })
    },
    editTag (id) {
      this.tagID = id
      this.dialogVisible = true
      this.$refs.dialogRef.editTag(id)
    }
  }
}
</script>

<style scoped>
.el-alert {
  margin-bottom: 20px;
}
</style>
